package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.api.UUser;
import com.ruoyi.system.service.IUUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.api.WRecordRecharge;
import com.ruoyi.system.service.IWRecordRechargeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充值记录Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/api/wRecordRecharge")
public class WRecordRechargeController extends BaseController
{
    @Autowired
    private IWRecordRechargeService wRecordRechargeService;

    @Autowired
    private IUUserService userService;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(WRecordRecharge wRecordRecharge)
    {
        if (!StringUtils.isEmpty(wRecordRecharge.getUserEmail())){
            UUser user = new UUser();
            user.setEmail(wRecordRecharge.getUserEmail());

            List<UUser> users = userService.selectUUserList(user);

            if (!users.isEmpty()){
                wRecordRecharge.setUserId(users.get(0).getId());
            }else {
                wRecordRecharge.setUserId(1l);
            }
        }
        startPage();
        List<WRecordRecharge> list = wRecordRechargeService.selectWRecordRechargeList(wRecordRecharge);
        if (!list.isEmpty()){
            for (WRecordRecharge u: list) {
                UUser user = userService.selectUUserById(u.getUserId());

                if (user != null){
                    u.setUserEmail(user.getEmail());
                }

            }
        }
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:export')")
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WRecordRecharge wRecordRecharge)
    {
        List<WRecordRecharge> list = wRecordRechargeService.selectWRecordRechargeList(wRecordRecharge);
        ExcelUtil<WRecordRecharge> util = new ExcelUtil<WRecordRecharge>(WRecordRecharge.class);
        util.exportExcel(response, list, "充值记录数据");
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(wRecordRechargeService.selectWRecordRechargeById(id));
    }

    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WRecordRecharge wRecordRecharge)
    {
        return toAjax(wRecordRechargeService.insertWRecordRecharge(wRecordRecharge));
    }

    /**
     * 修改充值记录
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:edit')")
    @Log(title = "充值记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WRecordRecharge wRecordRecharge)
    {
        return toAjax(wRecordRechargeService.updateWRecordRecharge(wRecordRecharge));
    }

    /**
     * 删除充值记录
     */
    @PreAuthorize("@ss.hasPermi('api:wRecordRecharge:remove')")
    @Log(title = "充值记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wRecordRechargeService.deleteWRecordRechargeByIds(ids));
    }
}
